﻿using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace ioal
{
    //const int TABLE = 2;
	public interface ICore
	{
		void login();
        bool old();
		int status();
		int error_code();
		void begin_game();
		void end_game();
		void logout();
	};

	public interface IGUI
	{
		int login_progress();				//login进度(0~100)
		int online_players();
		String status_string();		    	//状态描述字符串
		String error_string();				//错误描述字符串,code为0代表最近发生的错误码
		
		void msg_state_change(Form form);
		String get_information(int item);
	};

	public class ioal : ICore, IGUI
	{
        public ioal(UInt16 port, String server)
        {
            m_ioal = ioal_wrapper.ioal_create(port, server);
        }

        public void login()
        {
            ioal_wrapper.ioal_login_async(m_ioal);
        }

        public bool old()
        {
            return ioal_wrapper.ioal_old(m_ioal)!=0;
        }

		public int status()
        {
            return ioal_wrapper.ioal_status(m_ioal);
        }

		public int error_code()
        {
            return ioal_wrapper.ioal_error_code(m_ioal);
        }

		public void begin_game()
        {
            ioal_wrapper.ioal_begin_game(m_ioal);
        }

		public void end_game()
        {
            ioal_wrapper.ioal_end_game(m_ioal);
        }

		public void logout()
        {
            ioal_wrapper.ioal_logout(m_ioal);
        }

		public int login_progress()
        {
            return ioal_wrapper.ioal_login_progress(m_ioal);
        }

		public int online_players()
        {
            return ioal_wrapper.ioal_online_players(m_ioal);
        }

		public String status_string()
        {
            return ioal_wrapper.ioal_status_string(m_ioal);
        }

		public String error_string()
        {
            return ioal_wrapper.ioal_error_string(m_ioal);
        }

        public const int OnStateChange = 0x0400 + 930;
		public void msg_state_change(Form form)
        {
            ioal_wrapper.ioal_msg_state_change(m_ioal, form.Handle);
        }

        public String get_information(int item)
        {
            return ioal_wrapper.ioal_get_information(m_ioal, item);
        }
	
		private UInt32 m_ioal;
    };

	public struct node_info
	{
		public String nick;
        public uint ip, vip, ping;
        public String comment;
	};

    public class ioal_wrapper
    {
        public const int Ok = 0;
        public const int TapFailed = 1;
        public const int NetworkFailed = 2;
        public const int ServerConnectionFailed = 3;
        public const int IPFailed = 4;
        public const int ClientVersionTooOld = 5;

        [DllImport("ioal.dll")]
        public static extern UInt32 ioal_create(UInt16 port, String server);

        [DllImport("ioal.dll")]
        public static extern void ioal_login(UInt32 ioal);
        
        [DllImport("ioal.dll")]
        public static extern void ioal_login_async(UInt32 ioal);
        
        [DllImport("ioal.dll")]
        public static extern int ioal_login_progress(UInt32 ioal);
        
        [DllImport("ioal.dll")]
        public static extern int ioal_online_players(UInt32 ioal);
        
        [DllImport("ioal.dll")]
        public static extern void ioal_msg_state_change(UInt32 ioal, IntPtr hwnd);
        
        [DllImport("ioal.dll")]
        public static extern int ioal_status(UInt32 ioal);
        
        [DllImport("ioal.dll")]
        public static extern String ioal_status_string(UInt32 ioal);
        
        [DllImport("ioal.dll")]
        public static extern int ioal_error_code(UInt32 ioal);
        
        [DllImport("ioal.dll")]
        public static extern String ioal_error_string(UInt32 ioal);
        
        [DllImport("ioal.dll")]
        public static extern int ioal_old(UInt32 ioal);
        
        [DllImport("ioal.dll")]
        public static extern void ioal_begin_game(UInt32 ioal);
        
        [DllImport("ioal.dll")]
        public static extern void ioal_end_game(UInt32 ioal);
        
        [DllImport("ioal.dll")]
        public static extern void ioal_logout(UInt32 ioal);
        
        [DllImport("ioal.dll")]
        public static extern void ioal_delete(UInt32 ioal);

        public const int VIP=0;
        public const int VMASK=1;
        [DllImport("ioal.dll")]
        public static extern String ioal_get_information(UInt32 ioal, int item);
    }
}
